package gspushmq.v1.produce.job.hq;

import java.util.List;
import java.util.Set;

import org.java_websocket.WebSocket;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import gspushmq.model.Message;
import gspushmq.model.MessageType;
import gspushmq.util.GGLogger;
import gspushmq.v1.subscribe.GGSubscribe;

/**
 * @Description 监控服务
 * @Author 成尚佳
 * @Date 2018/12/24 16:04
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class MonitorService {

    public static void doMonitor(WebSocket ws) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("----->监控日志--------->").append("<br/>")
                .append("在线ws用户：<br/>").append(JSON.toJSONString(GGSubscribe.wstUser)).append("<br/>")
                .append("订阅360周报的在线用户有：<br/>").append(getMessages("sys", "wreport_360")).append("<br/>")
                ;
        ws.send(buffer.toString());
    }


    public static String getMessages(String mqType, String mqSubType) {
        MessageType messageType = new MessageType();
        messageType.setMq_sub_type(mqSubType);
        messageType.setMq_type(mqType);
        return messageUnit(messageType);
    }


    public static String messageUnit(MessageType messageType) {
        Set<Message> messageSet = GGSubscribe.messWocketMap.get(messageType);
        StringBuffer buffer = new StringBuffer();
        if (messageSet != null) {
            for (Message message : messageSet) {
                try {
                    if (message.getMq_sub_type().equals(messageType.getMq_sub_type())) {
                        buffer.append("uid:").append(message.getAccount_id()).append(" | ")
                                .append("pro:").append(message.getPro_type()).append(" | ")
                                .append("productLine:").append(message.getProduct_line()).append(" | ")
                                .append("ws:").append(message.getWst() == null ? null : message.getWst().isOpen()).append(" | ")
                                .append("hashcode:").append(message.hashCode())
                        .append("<br/>");
                    }
                } catch (Exception e) {
                    return e.getMessage();
                }
            }
        }
        return buffer.toString();
    }

    public static void  onlineMonitor(){
        List<String> reList = Lists.newArrayList();

        for (Set<Message> set : GGSubscribe.messWocketMap.values()){
            for (Message message :set){
                reList.add(new StringBuffer().append(message.getMq_sub_type()).append("|")
                        .append("uid:").append(message.getAccount_id()).append("|")
                        .append("productLine:").append(message.getProduct_line()).append("|")
                        .append("ws:").append(message.getWst() == null ? null : message.getWst().isOpen()).toString());
            }
        }
        GGLogger.warn(MonitorService.class,JSON.toJSONString(reList));
    }
}
